package com.lagou.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lagou.pojo.User;
import com.lagou.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * @author lbq
 * @version 1.0
 * @date 2021/1/27 21:16
 */
@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/toLogin")
    public String toLogin() {
        return "login";
    }

    @RequestMapping("/login/loginSystem")
    public String login(String username, String password, HttpSession session) {
        if (username == null || password == null) {
            return "login";
        }
        User user = userService.getOne(new QueryWrapper<>(new User().setName(username).setPassword(password)));
        // 合法用户，信息写入session，同时跳转到系统主页面
        if (user != null) {
            System.out.println("合法用户");
            session.setAttribute("loginUser", username);
            return "result";
        } else {
            // 非法用户返回登录页面
            System.out.println("非法，跳转");
            return "redirect:/toLogin";
        }
    }

}
